<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/1/13
 * Time: 16:04
 */

namespace SDXApi\Repositories;


use SDXApi\Common\Tools\AppVersion;
use SDXApi\Listeners\LocationLinkSourceListener;
use SDXApi\Model\CategoryModel;
use SDXApi\Model\GoodsModel;
use SDXApi\Model\TouchCategoryModel;

/**
 * Class CategoryRepository
 * @package SDXApi\Repositories
 */
class CategoryRepository
{
	/**
	 * @var CategoryModel
	 */
	protected $categoryModel;

	/**
	 * @var TouchCategoryModel
	 */
	protected $touchCategoryModel;

	/**
	 * @var GoodsModel
	 */
	protected static $goodsModel;

    /**
     * @var CategoryModel
     */
    protected static $category;

	/**
	 * CategoryRepository constructor.
	 */
	public function __construct()
	{
		$this->categoryModel = new CategoryModel();
		$this->touchCategoryModel = new TouchCategoryModel();
		self::$goodsModel = new GoodsModel();
		self::$category = $this->categoryModel;
	}

	/**
	 * Created by Jerry
	 * @return array|bool
	 */
	public function getCategoryParentList()
	{
		$data = $this->findCategory();

		$ads = $this->categoryModel->getCategoryAds();


		$category = self::categoryForLevel($data, $ads);

		return $category;
	}


	/**
	 * 获取指定分类下所有子分类
	 * Created by Jerry
	 * @param $id
	 * @return array|bool
	 */
	public function checkCategoryById($id)
	{
		if (empty($id)) {
			return false;
		}

		$data = $this->getCategoryById($id);

		if (empty($data)) {
			return false;
		}

		$categories = self::categoryForLevel($data, 'child', $id);

		return $categories;
	}

	/**
	 * Created by Jerry
	 * @param $id
	 * @return bool|mixed
	 */
	protected function getCategoryById($id)
	{
		if (empty($id)) {
			return false;
		}

		return $this->categoryModel->findCategoryByCatId($id);
	}

	/**
	 * Created by Jerry
	 * @return mixed
	 */
	protected function findCategory()
	{
		return $this->categoryModel->findCategoryList();
	}

    /**
     * @author Jerry
     * @param array $data
     * @param array $ads
     * @param string $name
     * @param int $parentId
     * @param int $level
     * @return array|bool
     */
	static protected function categoryForLevel($data = array(), $ads = array(), $name = 'child', $parentId = 0, $level = 0)
	{
	    $wid = I('post.wid', 8, 'int');

		if (empty($data)) {
			return false;
		}

		$arr = array();
		$level += 1;
		foreach ($data as $key => $value) {

            if ($value['cat_id'] == 839) {
                if (self::checkAppNotData()) {
                    continue;
                }
            }

            if (empty($value['cat_name'])) {
                continue;
            }

            $ad = self::getSearchArrayValue($ads, 'cat_id', $value['cat_id']);
            if (! empty($ad)) {
                $value['ads_data'] = $ad;
            }

//		    安卓万能跳转
		    if (! empty($value['link'])) {
		        $linkListener = new LocationLinkSourceListener();
		        $androidLink = $linkListener->onCallLink($value['link']);
		        if (! empty($androidLink)) {
                    $value['link_data'] = $androidLink;
                }
            }

			if ($value['parent_id'] == $parentId) {
				if ($level > 3) {
					continue;
				}

				if ($level == 2) {
                    $count = self::getCategoryGoodsCount($value['cat_id'], $wid);

                    $categoryCount = self::getCategoryParentNumber($value['cat_id']);

                    if ($count == 0 && $categoryCount == 0 && empty($value['link']) && empty($value['keywords'])) {
                        continue;
                    }
                }

				if ($level == 3) {
					$count = self::getCategoryGoodsCount($value['cat_id'], $wid);

					if (AppVersion::checkAppVersion('ios', '2.3.73', '>=')) {
                        if ($count == 0 && empty($value['link']) && empty($value['keywords'])) {
                            continue;
                        }
                    }
                    else if (AppVersion::checkAppName('h5')) {
                        if ($count == 0 && empty($value['link']) && empty($value['keywords'])) {
                            continue;
                        }
                    }
                    else{
                        if ($count == 0) {
                            continue;
                        }
                    }

				}

				$value['level'] = $level;

				if ($level != 3) {
					//unset($value['cat_image']);
				}

				$value[$name] = self::categoryForLevel($data, $ads, $name, $value['cat_id'], $level);
				$arr[] = $value;
			}
		}

		return $arr;
	}

    /**
     * @author Jerry
     * @param array $ads
     * @param string $searchKey
     * @param string $searchValue
     * @return bool|mixed
     */
    static protected function getSearchArrayValue($ads = array(), $searchKey = '', $searchValue = '')
    {
        if (empty($ads)) {
            return false;
        }

        foreach ($ads as $key => $value) {
            if ($value[$searchKey] == $searchValue) {
                return $value;
            }
        }

        return false;
    }

    /**
     * 获取分类下商品总数
     * @author Jerry
     * @param int $category_id
     * @param int $wid
     * @return mixed
     */
	static protected function getCategoryGoodsCount($category_id = 0, $wid = 8)
	{
		return self::$goodsModel->findCategoryGoodsCountByCategoryId($category_id, $wid);
	}

    /**
     * @author Jerry
     * @param int $category_id
     * @return mixed
     */
    static protected function getCategoryParentNumber($category_id = 0)
    {
        $number = self::$category->getCategoryParentCountNumber($category_id);
        return $number + 0;
    }


	/**
	 * Created by Jerry
	 * @param $categoryId
	 * @return bool|mixed
	 */
	public function findCategoryImage($categoryId)
	{
		if (empty($categoryId)) {
			return false;
		}

		return $this->touchCategoryModel->findImageByCategoryId($categoryId);
	}

    /**
     * @return bool
     */
	protected static function checkAppNotData()
    {

        if (AppVersion::checkAppVersion('android', '2.01')) {
            return true;
        }

        if (AppVersion::checkAppVersion('ios', '2.3.64')) {
            return true;
        }



        return false;
    }


}